<?php

namespace App\Admin\Controllers;

use App\Meals;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MealsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '饭点打卡记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Meals());
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置datetime类型
            $filter->equal('id', '编号');
            $filter->between('date', '活动时间')->date();


        });

        $grid->column('id', __('Id'));
        $grid->column('date', '编码');
        $grid->column('user_amount', '奖金池');
        $grid->column('base_amount', '初始金额')->editable();
        $grid->column('join_num', '签到/参与人数')->display(function($value){
            return "{$this->sign_num}/{$value}";
        });
        $grid->column('sign_amount', '平分资格数');
        $grid->column('sign_amount', '平分基数')->display(function($value){
            return $value?round($this->user_amount / $value, 2):0;
        });
        $grid->disableActions();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Meals::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('date', __('Date'));
        $show->field('base_amount', __('Base amount'));
        $show->field('user_amount', __('User amount'));
        $show->field('join_num', __('Join num'));
        $show->field('sign_num', __('Sign num'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Meals());

        $form->date('date', __('Date'))->default(date('Y-m-d'));
        $form->number('base_amount', __('Base amount'));
        $form->number('user_amount', __('User amount'));
        $form->number('join_num', __('Join num'));
        $form->number('sign_num', __('Sign num'));

        return $form;
    }
}
